package com.blog.controller;

import com.blog.bean.Article;
import com.blog.bean.ArticleCategory;
import com.blog.service.ArticleCategoryService;
import com.blog.service.ArticleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class IndexController {

    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleCategoryService articleCategoryService;

    /**
     * 首页，加载主分类
     * 加载最新发布8篇
     *
     * @param model
     * @return
     */
    @RequestMapping(value = {"", "/", "/index"})
    public String index(Model model) {
        ArticleCategory articleCategory = new ArticleCategory();
        articleCategory.setArticleCategoryParentId((long) 0);
        articleCategory.setArticleCategoryState(0);
        List<ArticleCategory> articleCategoryList = articleCategoryService.selectArticleCategory(articleCategory);
        List<Article> articles = articleService.selectArticleByUpdate();
        model.addAttribute("articleCategoryList", articleCategoryList);
        model.addAttribute("articles", articles);
        return "index";
    }

    /**
     * 首页，加载次分类
     *
     * @param articleCategoryId
     * @param model
     * @return
     */
    @RequestMapping("/articleCategoryFirst")
    public String articleCategoryFirst(@RequestParam("articleCategoryId") Long articleCategoryId, Model model) {
        // 次分类信息
        ArticleCategory ac = new ArticleCategory();
        ac.setArticleCategoryId(articleCategoryId);
        ac.setArticleCategoryState(0);
        ArticleCategory articleCategory = articleCategoryService.getArticleCategory(ac);
        model.addAttribute("articleCategory", articleCategory);
        // 次分类列表
        ArticleCategory temp = new ArticleCategory();
        temp.setArticleCategoryParentId(articleCategoryId);
        temp.setArticleCategoryState(0);
        List<ArticleCategory> articleCategoryList = articleCategoryService.selectArticleCategory(temp);
        model.addAttribute("articleCategoryList", articleCategoryList);
        return "article-category";
    }

    /**
     * 首页，加载三级文章列表
     *
     * @param articleCategoryId
     * @param model
     * @return
     */
    @RequestMapping("/articleCategorySecond")
    public String articleCategorySecond(@RequestParam("articleCategoryId") Long articleCategoryId, Model model) {
        // 次分类信息
        ArticleCategory ac = new ArticleCategory();
        ac.setArticleCategoryId(articleCategoryId);
        ac.setArticleCategoryState(0);
        ArticleCategory articleCategory = articleCategoryService.getArticleCategory(ac);
        model.addAttribute("articleCategory", articleCategory);
        // 主分类信息
        ArticleCategory fatherTemp = new ArticleCategory();
        fatherTemp.setArticleCategoryId(articleCategory.getArticleCategoryParentId());
        ArticleCategory articleCategoryFather = articleCategoryService.getArticleCategory(fatherTemp);
        model.addAttribute("articleCategoryFather", articleCategoryFather);
        // 次分类列表
        Article article = new Article();
        article.setArticleParentId(articleCategoryId);
        article.setArticleState(0);
        List<Article> articleList = articleService.selectArticle(article);
        model.addAttribute("articleList", articleList);
        return "article-list";
    }

    /**
     * 首页，加载文章详情
     * 同时加载该文章同级的所有文章
     *
     * @param articleId
     * @param model
     * @return
     */
    @RequestMapping("/articleDetail")
    public String articleDetail(@RequestParam(value = "articleId") Long articleId, Model model) {
        // 文章信息
        Article ar = new Article();
        ar.setArticleId(articleId);
        Article article = articleService.getArticle(ar);
        model.addAttribute("article", article);
        // 同级文章列表
        Article temp = new Article();
        temp.setArticleParentId(article.getArticleParentId());
        List<Article> articleList = articleService.selectArticle(temp);
        model.addAttribute("articleList", articleList);
        // 文章次分类
        ArticleCategory acSecond = new ArticleCategory();
        acSecond.setArticleCategoryId(article.getArticleParentId());
        ArticleCategory articleCategorySecond = articleCategoryService.getArticleCategory(acSecond);
        model.addAttribute("articleCategorySecond", articleCategorySecond);
        // 文章主分类
        ArticleCategory acFirst = new ArticleCategory();
        acFirst.setArticleCategoryId(articleCategorySecond.getArticleCategoryParentId());
        ArticleCategory articleCategoryFirst = articleCategoryService.getArticleCategory(acFirst);
        model.addAttribute("articleCategoryFirst", articleCategoryFirst);
        return "article-detail";
    }
}
